/**
 * 监控服务
 * 处理系统监控相关业务逻辑
 */

import { request } from '@/utils/request'
import { API_ENDPOINTS } from '@/constants'
import type { OnlineUser, LoginLog, OperLog } from '@/types/monitor'

export class MonitorService {
  /**
   * 获取在线用户列表
   */
  static async getOnlineUsers(params?: { page?: number; size?: number }) {
    try {
      const response = await request.get(API_ENDPOINTS.ONLINE_USERS, { params })
      return response.data
    } catch (error) {
      console.error('获取在线用户列表失败:', error)
      throw error
    }
  }

  /**
   * 强制下线用户
   */
  static async forceLogout(sessionId: string): Promise<void> {
    try {
      await request.delete(`${API_ENDPOINTS.ONLINE_USERS}/${sessionId}`)
    } catch (error) {
      console.error('强制下线用户失败:', error)
      throw error
    }
  }

  /**
   * 批量强制下线用户
   */
  static async batchForceLogout(sessionIds: string[]): Promise<void> {
    try {
      await request.delete(API_ENDPOINTS.ONLINE_USERS, { data: { sessionIds } })
    } catch (error) {
      console.error('批量强制下线用户失败:', error)
      throw error
    }
  }

  /**
   * 获取登录日志列表
   */
  static async getLoginLogs(params?: {
    page?: number
    size?: number
    username?: string
    status?: string
    startTime?: string
    endTime?: string
  }) {
    try {
      const response = await request.get(API_ENDPOINTS.LOGIN_LOGS, { params })
      return response.data
    } catch (error) {
      console.error('获取登录日志列表失败:', error)
      throw error
    }
  }

  /**
   * 获取登录日志详情
   */
  static async getLoginLogDetail(id: string): Promise<LoginLog> {
    try {
      const response = await request.get(`${API_ENDPOINTS.LOGIN_LOGS}/${id}`)
      return response.data
    } catch (error) {
      console.error('获取登录日志详情失败:', error)
      throw error
    }
  }

  /**
   * 删除登录日志
   */
  static async deleteLoginLog(id: string): Promise<void> {
    try {
      await request.delete(`${API_ENDPOINTS.LOGIN_LOGS}/${id}`)
    } catch (error) {
      console.error('删除登录日志失败:', error)
      throw error
    }
  }

  /**
   * 批量删除登录日志
   */
  static async batchDeleteLoginLogs(ids: string[]): Promise<void> {
    try {
      await request.delete(API_ENDPOINTS.LOGIN_LOGS, { data: { ids } })
    } catch (error) {
      console.error('批量删除登录日志失败:', error)
      throw error
    }
  }

  /**
   * 清空登录日志
   */
  static async clearLoginLogs(): Promise<void> {
    try {
      await request.delete(`${API_ENDPOINTS.LOGIN_LOGS}/clear`)
    } catch (error) {
      console.error('清空登录日志失败:', error)
      throw error
    }
  }

  /**
   * 获取操作日志列表
   */
  static async getOperLogs(params?: {
    page?: number
    size?: number
    title?: string
    businessType?: string
    status?: string
    startTime?: string
    endTime?: string
  }) {
    try {
      const response = await request.get(API_ENDPOINTS.OPER_LOGS, { params })
      return response.data
    } catch (error) {
      console.error('获取操作日志列表失败:', error)
      throw error
    }
  }

  /**
   * 获取操作日志详情
   */
  static async getOperLogDetail(id: string): Promise<OperLog> {
    try {
      const response = await request.get(`${API_ENDPOINTS.OPER_LOGS}/${id}`)
      return response.data
    } catch (error) {
      console.error('获取操作日志详情失败:', error)
      throw error
    }
  }

  /**
   * 删除操作日志
   */
  static async deleteOperLog(id: string): Promise<void> {
    try {
      await request.delete(`${API_ENDPOINTS.OPER_LOGS}/${id}`)
    } catch (error) {
      console.error('删除操作日志失败:', error)
      throw error
    }
  }

  /**
   * 批量删除操作日志
   */
  static async batchDeleteOperLogs(ids: string[]): Promise<void> {
    try {
      await request.delete(API_ENDPOINTS.OPER_LOGS, { data: { ids } })
    } catch (error) {
      console.error('批量删除操作日志失败:', error)
      throw error
    }
  }

  /**
   * 清空操作日志
   */
  static async clearOperLogs(): Promise<void> {
    try {
      await request.delete(`${API_ENDPOINTS.OPER_LOGS}/clear`)
    } catch (error) {
      console.error('清空操作日志失败:', error)
      throw error
    }
  }

  /**
   * 导出登录日志
   */
  static async exportLoginLogs(params?: Record<string, any>): Promise<Blob> {
    try {
      const response = await request.get(`${API_ENDPOINTS.LOGIN_LOGS}/export`, {
        params,
        responseType: 'blob'
      })
      return response.data
    } catch (error) {
      console.error('导出登录日志失败:', error)
      throw error
    }
  }

  /**
   * 导出操作日志
   */
  static async exportOperLogs(params?: Record<string, any>): Promise<Blob> {
    try {
      const response = await request.get(`${API_ENDPOINTS.OPER_LOGS}/export`, {
        params,
        responseType: 'blob'
      })
      return response.data
    } catch (error) {
      console.error('导出操作日志失败:', error)
      throw error
    }
  }
}
